import ActionTypes from '../config/ActionTypes';
import ActionCodes from '../config/ActionCodes';
import { postFetch, getFetch } from '../utils/network/request/HttpExtension.js';
import { createAction } from 'redux-actions';
import Session from '../utils/SessionUtils';
import { AsyncStorage } from 'react-native';
var filterArr = []

export function getOrgList(){
    return dispatch => {
        dispatch({ type: ActionTypes.OrgList + ActionTypes.FETCH_LOADING_SUFFIX });
		let param = {
			ISMOBILE: 1,
			// ORG_ID: '558b5464-b1ae-46ad-9809-6901c515301e'
        };

		postFetch(ActionCodes.orgList, param)
			.then(res => {
				let orgList = res.List;
				// orgList.forEach(o => {
				// 	fetchUsers(o.ORG_ID)
				// })
				filter(orgList)
				fetchUsers(filterArr[0].ORG_ID)

                // var items = []
                // if (res && JSON.stringify(res).indexOf('Public.E') < 0){
                //     items = res.List
                // }
				// dispatch({
				// 	type: ActionTypes.COMMENTLIST + ActionTypes.FETCH_SUCCESS_SUFFIX,
				// 	items: items
				// });
			})
			.catch(err => {
				alert(err);
				dispatch({ type: ActionTypes.COMMENTLIST + ActionTypes.FETCH_ERROR_SUFFIX });
			});
    }
}

const filter = arr => {
	(async() =>{
		filterArr = arr.filter( a => a.ORG_NAME == '开发测试')
	})()
}

const fetchUsers = orgId =>{
	let param = {
		ISMOBILE: 1,
		ORG_ID: orgId
	};

	postFetch(ActionCodes.employeeListByOrgID, param)
		.then(res => {
			// let OrgList = JSON.parse(res)
			console.log(JSON.stringify(res))
			// var items = []
			// if (res && JSON.stringify(res).indexOf('Public.E') < 0){
			//     items = res.List
			// }
			// dispatch({
			// 	type: ActionTypes.COMMENTLIST + ActionTypes.FETCH_SUCCESS_SUFFIX,
			// 	items: items
			// });
		})
		.catch(err => {
			alert(err);
			
		});
}